﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration; 
namespace LOGIN
{
    public partial class OutputTable : Form
    {
        public OutputTable()
        {
            InitializeComponent();
            this.dgv_OutputTable.AllowUserToAddRows = false;
            
        }
        private DataTable OutputTables;

        private void OutputTable_Load(object sender, EventArgs e)
        {
            this.label5.Visible = false;
            this.label7.Visible = false;
            this.label8.Visible = false;
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT c.MedicineID,c.YKNo,cs.YKName,DrugName,Guige,DW,MedicineSum,dbo.fn_GetPy(DrugName) AS pymc FROM dbo.medicineData c JOIN dbo.medicineWareHouse cs ON cs.YKNo=c.YKNo ;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            this.OutputTables = new DataTable();
            
            sqlConnection.Open();

            sqlDataAdapter.Fill(OutputTables);
            
            sqlConnection.Close();
            this.dgv_OutputTable.DataSource = OutputTables;
            dgv_OutputTable.Columns["MedicineID"].HeaderText = "ID";
            dgv_OutputTable.Columns["YKNo"].HeaderText = "药库ID";
            dgv_OutputTable.Columns["YKName"].HeaderText = "药库";
            dgv_OutputTable.Columns["DrugName"].HeaderText = "药品名";
            dgv_OutputTable.Columns["Guige"].HeaderText = "规格";
            dgv_OutputTable.Columns["DW"].HeaderText = "单位";
            dgv_OutputTable.Columns["MedicineSum"].HeaderText = "库存量";

            dgv_OutputTable.Columns["YKNo"].Visible = false;
            dgv_OutputTable.Columns["pymc"].Visible = false;
        }

        
        private void dgv_OutputTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (dgv_OutputTable.SelectedCells.Count != 0)
            {
                int index = dgv_OutputTable.CurrentRow.Index;
                this.Medicine.Text = dgv_OutputTable.Rows[index].Cells[3].Value.ToString();
                this.Num.Text = dgv_OutputTable.Rows[index].Cells[6].Value.ToString();
                this.house.Text = dgv_OutputTable.Rows[index].Cells[2].Value.ToString();
                this.label5.Text = dgv_OutputTable.Rows[index].Cells[6].Value.ToString();
                this.label7.Text = dgv_OutputTable.Rows[index].Cells[0].Value.ToString();
                this.label8.Text = dgv_OutputTable.Rows[index].Cells[1].Value.ToString();
            }
        }

        private void btn_Output_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "")
            {
                MessageBox.Show("请选择退库方式");
            }
            else
            {
                SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString =
                    ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
                SqlCommand updateCommand = new SqlCommand();
                updateCommand.Connection = sqlConnection;
                updateCommand.CommandText =
                    "UPDATE medicineData"
                    + " SET MedicineSum=@MedicineSum"
                    + " WHERE MedicineID=@MedicineID;";
                updateCommand.Parameters.Add("@MedicineID", this.label7.Text);
                int a = Convert.ToInt32(this.label5.Text.Trim()) - Convert.ToInt32(this.Num.Text.Trim());
                updateCommand.Parameters.AddWithValue("@MedicineSum", a.ToString());


                SqlCommand insertCommand = new SqlCommand();
                insertCommand.Connection = sqlConnection;
                insertCommand.CommandText =
                    "INSERT OutputTable"
                    + "(YKNo,MedicineID,OutputNum,OutputDate,Style,UserID)"
                    + " VALUES(@YKNo,@MedicineID,@OutputNum,@OutputDate,@Style,@UserID);";

                insertCommand.Parameters.AddWithValue("@YKNo", this.label8.Text.Trim());
                insertCommand.Parameters.AddWithValue("@MedicineID", this.label7.Text.Trim());
                insertCommand.Parameters.AddWithValue("@OutputNum", this.Num.Text.Trim());
                insertCommand.Parameters.AddWithValue("@OutputDate", DateTime.Now.ToString());
                insertCommand.Parameters.AddWithValue("@Style", comboBox1.Text);
                insertCommand.Parameters.AddWithValue("@UserID", ordersHelper.UserID);

                sqlConnection.Open();
                int rowAffected = updateCommand.ExecuteNonQuery();
                int rowAffected1 = insertCommand.ExecuteNonQuery();
                sqlConnection.Close();
                MessageBox.Show("药品表更新" + rowAffected.ToString() + "行。");
                MessageBox.Show("退库单更新" + rowAffected1.ToString() + "行。");
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataRow[] searchResultRows =
                this.OutputTables.Select("pymc LIKE '%" + this.textBox1.Text.Trim() + "%'");        
            DataTable searchResultTable = this.OutputTables.Clone();                                        
            foreach (DataRow row in searchResultRows)                                                      
            {
                searchResultTable.ImportRow(row);                                                         
            }
            this.dgv_OutputTable.DataSource = searchResultTable;    
        }

        private void dgv_OutputTable_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        

      
    }
}
